package com.liujit.bms.pc.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujit.bms.pc.domain.model.BlogArticleLabel;
import com.liujit.bms.pc.domain.model.BlogLabel;
import com.liujit.bms.pc.domain.result.BlogLabelResult;
import com.liujit.bms.pc.mapper.BlogArticleLabelMapper;
import com.liujit.bms.pc.service.BlogArticleLabelService;
import com.liujit.bms.pc.service.BlogLabelService;
import com.liujit.common.constants.GlobalConstant;
import com.liujit.common.utils.DozerConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description 博客文章标签Service实现类
 * @Author liujun
 * @Date 2021-07-03 14:36:28
 */
@Service
public class BlogArticleLabelServiceImpl extends ServiceImpl<BlogArticleLabelMapper, BlogArticleLabel>
        implements BlogArticleLabelService {

    private BlogLabelService blogLabelService;

    @Autowired
    public void setBlogLabelService(BlogLabelService blogLabelService) {
        this.blogLabelService = blogLabelService;
    }

    @Override
    public List<Long> getArticleIdByLabelId(Long labelId) {
        LambdaQueryWrapper<BlogArticleLabel> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(BlogArticleLabel::getArticleId);
        wrapper.eq(BlogArticleLabel::getLabelId, labelId);
        return super.listObjs(wrapper, item -> Convert.toLong(item));
    }

    @Override
    public List<BlogLabelResult> getListByArticleId(Long articleId) {
        LambdaQueryWrapper<BlogArticleLabel> wrapper = new LambdaQueryWrapper();
        wrapper.select(BlogArticleLabel::getLabelId);
        wrapper.eq(BlogArticleLabel::getArticleId, articleId);
        List<Long> ids = super.listObjs(wrapper, item -> Convert.toLong(item));
        if (CollectionUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        LambdaQueryWrapper<BlogLabel> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(BlogLabel::getEnabled, GlobalConstant.ENABLED_YES);
        queryWrapper.in(BlogLabel::getId, ids);
        List<BlogLabel> list = blogLabelService.list(queryWrapper);
        return DozerConvertor.convertor(list, BlogLabelResult.class);
    }
}
